﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.Repository;
using Estimate.Common.DBFieldTypes;
using Estimate.Common;
using System.Data.SqlClient;
using Estimate.Common.Estimate;

namespace Estimate.Common.OldEstimate
{
    public class OldForeTableRepository
    {
        public static void GetValueAndSemi(string connectionString, string tableName, DateTime dt, ForeClockType clock,ForeSendpointType sendpoint, ForeSpanType span, ForeFactorType factor,out string value, out string semi)
        {
            value = "";
            semi = "";
            string sql = string.Format("SELECT value, semi FROM {0} WHERE date=@date and clock = @clock and span = @span and type=@type",OldTableNameFactory.GetOldTableName(tableName,dt,sendpoint));
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@date", dt.Date.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@clock", OldTableNameFactory.GetClockField(clock));
            param[2] = new SqlParameter("@span",  OldTableNameFactory.GetSpanField(span));
            param[3] = new SqlParameter("@type", OldTableNameFactory.GetTypeField(span,factor));
            
            

            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataReader reader = DBHelper.ExecuteReader(connection, sql,param);
            if(reader.Read())
            {
                value =  reader["value"].ToString();
                semi = reader["semi"].ToString();  
                connection.Close();
            }
        }

        public static string GetRealValue(string connectionString, DateTime date, ForeFactorType factor, ForeSpanType span, ForeClockType clock)
        {
            ForeRealLinker linker = new ForeRealLinker();
            if (factor == ForeFactorType.wth)
            {
                RainForeRealCompare comparer = linker.GetForeRelatedRainCondition(date, span, clock);
                string type = comparer.RainType.ToString().Replace("D", "");
                string tableName = string.Format("{0}_RainReal", comparer.RealDate.ToString("yyyy"));
                string sql = string.Format("SELECT *  FROM [TestEstimate].[dbo].[{0}] WHERE date='{1}' and type='{2}'", tableName, comparer.RealDate.ToString("yyyyMMdd"), type);
                SqlConnection connection = new SqlConnection(connectionString);
                SqlDataReader reader = DBHelper.ExecuteReader(connection, sql);

                string result = string.Empty;

                if (reader.Read())
                {
                    result = reader["value"].ToString();
                }
                connection.Close();
                return result;
            }
            else if (factor == ForeFactorType.maxt || factor == ForeFactorType.mint)
            {

                TempForeRealCompare tempComparer = linker.GetForeRelatedTempCondition(date, span, clock, factor);
                string tableName = string.Format("{0}_TempReal", tempComparer.RealDate.ToString("yyyy"));
                string type = tempComparer.TempType.ToString().Replace("D", "");
                string sql = string.Format("SELECT *  FROM [TestEstimate].[dbo].[{0}] WHERE date='{1}' and type='{2}'", tableName, tempComparer.RealDate.ToString("yyyyMMdd"), type);
                SqlConnection connection = new SqlConnection(connectionString);
                SqlDataReader reader = DBHelper.ExecuteReader(connection, sql);

                string result = string.Empty;

                if (reader.Read())
                {
                    result = reader["value"].ToString();
                }
                connection.Close();
                return result;
            }
            else
                return "";
        }
        


    }
}
